﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;

namespace WinQuickIP
{
    public partial class MainForm : Form
    {
        private List<IPProfile> ipProfiles;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            ipProfiles = IPUtils.LoadProfiles();
            lstProfiles.DisplayMember = "Name";
            lstProfiles.ValueMember = "IP";
            lstProfiles.DataSource = ipProfiles;
            lstProfiles.SelectedIndex = -1;

            var adapters = IPUtils.GetAdapters();
            cmbAdapter.DisplayMember = "Description";
            cmbAdapter.ValueMember = "Index";
            cmbAdapter.DataSource = adapters;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private IPRepresentation GetIpRepresentation()
        {
            if (cmbAdapter.SelectedValue == null) return null;
            return new IPRepresentation
            {
                Adapter = (uint)cmbAdapter.SelectedValue,
                DHCP = radDHCP.Checked,
                IP = txtIP.Text,
                Netmask = txtNetmask.Text,
                Gateway = txtGateway.Text,
                DNS1 = txtDns1.Text,
                DNS2 = txtDns2.Text
            };
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            var ip = GetIpRepresentation();
            try
            {
                bool requireReboot = IPUtils.ChangeIP(ip);
                if (requireReboot)
                    MessageBox.Show("Reboot required.", "Adapter settings successfully altered", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error setting IP for adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void radStatic_CheckedChanged(object sender, EventArgs e)
        {
            grpStaticSettings.Enabled = radStatic.Checked;
            if (radDHCP.Checked)
            {
                txtIP.Text = "";
                txtNetmask.Text = "";
                txtGateway.Text = "";
                txtDns1.Text = "";
                txtDns2.Text = "";
            }
        }

        private void cmbAdapter_SelectedIndexChanged(object sender, EventArgs e)
        {
            var ip = IPUtils.GetIPSettings((uint)cmbAdapter.SelectedValue);
            LoadIPSettings(ip);
        }

        private void LoadIPSettings(IPRepresentation ip)
        {
            if (ip == null)
                return;
            radDHCP.Checked = ip.DHCP;
            radStatic.Checked = !ip.DHCP;
            txtIP.Text = ip.IP;
            txtNetmask.Text = ip.Netmask;
            txtGateway.Text = ip.Gateway;
            txtDns1.Text = ip.DNS1;
            txtDns2.Text = ip.DNS2;
        }

        private void linkAbout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new AboutWinQuickIP().ShowDialog();
        }

        private void lstProfiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            var ipRep = (IPRepresentation)lstProfiles.SelectedValue;
            LoadIPSettings(ipRep);
        }

        private void btnSaveProfile_Click(object sender, EventArgs e)
        {
            ProfileNameForm profileNameForm = new ProfileNameForm();
            profileNameForm.ShowDialog();
            if (profileNameForm.DialogResult == DialogResult.Cancel)
                return;
            string profileName = profileNameForm.ProfileName;
            var ip = GetIpRepresentation();
            var ipProfile = new IPProfile { IP = ip, Name = profileName };
            ipProfiles.Add(ipProfile);
            lstProfiles.DataSource = null;
            lstProfiles.DisplayMember = "Name";
            lstProfiles.ValueMember = "IP";
            lstProfiles.DataSource = ipProfiles;
            lstProfiles.SelectedIndex = lstProfiles.Items.Count - 1;
            IPUtils.SaveProfiles(ipProfiles);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (ipProfiles.Count == 0 || lstProfiles.SelectedIndex == -1)
                return;
            ipProfiles.RemoveAt(lstProfiles.SelectedIndex);
            IPUtils.SaveProfiles(ipProfiles);
            lstProfiles.DataSource = null;
            lstProfiles.DisplayMember = "Name";
            lstProfiles.ValueMember = "IP";
            lstProfiles.DataSource = ipProfiles;
        }

        private void StaticSettingsChanged(object sender, EventArgs e)
        {
            var ip = GetIpRepresentation();

            foreach (var profile in ipProfiles)
            {
                if (!profile.IP.Equals(ip))
                    continue;
                lstProfiles.SelectedValue = profile.IP;
                return;
            }

            lstProfiles.SelectedIndex = -1;
        }
    }
}
